package com.generator.service.impl;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.generator.DTO.GeneratorDTO;
import com.generator.dao.TableListDao;
import com.generator.entity.TableColumn;
import com.generator.entity.TableListEntity;
import com.generator.service.TableListService;
import com.generator.utils.GeneratorUtils;
import com.generator.utils.PageData;
import com.generator.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author Xing
 * @Date 2023/8/9 9:21
 */
@Service
public class TableListServiceImpl implements TableListService {
    @Autowired
    private TableListDao dao;
    @Autowired
    private GeneratorUtils utils;

    @Override
    public Result getTableList(Map<String, Object> params) {
        String page = (String) params.get("page");
        List<TableListEntity> list = dao.GetTableList(dao.getDatabaseName(), page);
        Long tableNumber = dao.GetTableNumber(dao.getDatabaseName());
        PageData pageData = new PageData(list, tableNumber);
        return new Result().ok(pageData);
    }

    @Override
    public Result getTable(String tableName) {
        List<TableColumn> tableColumns = dao.GetColumns(tableName, dao.getDatabaseName());
        return new Result().ok(tableColumns);
    }

    public void generate(GeneratorDTO dto) {
        GeneratorDTO generator = dao.getGenerator();
        generator.setTableName(dto.getTableName());
        System.out.println(generator);
        try{
            utils.generator(generator);
        }catch (Exception e){
            e.fillInStackTrace();
        }
        System.out.println(123);
    }

    @Override
    public Result change(GeneratorDTO dto) {
        Boolean chang = dao.chang(dto);
        if (chang) {
            return new Result().ok();
        }
        return new Result().err(444);
    }

    @Override
    public Result GetSetting() {
        GeneratorDTO generator = dao.getGenerator();
        return new Result().ok(generator);
    }
}
